/*
 * AppListModel.java
 *
 * Created on 5. Februar 2007, 14:49
 *
 *  This file is part of JSmex.
 *
 *  JSmex is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  JSmex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package jsmex.function;

import javax.swing.AbstractListModel;
import jsmex.*;
import opencard.core.util.HexString;

/**
 *
 * @author Tobias Senger
 */
public class AppListModel extends AbstractListModel{
    
    private CardApplication[] ca;
    
    /** Creates a new instance of AppListModel */
    public AppListModel(CardApplication[] ca) {
        this.ca = ca;
    }

    public int getSize() {
        return ca.length;
    }

    public Object getElementAt(int index) {
        return HexString.hexify(ca[index].getAID());
    }
    
    public Object getLifeCycleState(int index) {
        if (ca[index].getSubset()!=(byte)0x80) {
            switch (ca[index].getLifeCycleState()) {
                case (byte)0x01: return new String("Loaded");
                case (byte)0x03: return new String("Installed");
                case (byte)0x07: return new String("Selectable");
                case (byte)0x83: return new String("Locked");
            }
        }
        else {
            switch (ca[index].getLifeCycleState()) {
                case (byte)0x01: return new String("OP Ready");
                case (byte)0x07: return new String("Initialized");
                case (byte)0x0F: return new String("Secured");
                case (byte)0x7F: return new String("Card Locked");
                case (byte)0xFF: return new String("Terminated");
            }
        }
        return HexString.hexify(ca[index].getLifeCycleState());
    }
    
    public Object getPrivileges(int index) {
        StringBuffer sb = new StringBuffer();
        if ((byte)(ca[index].getPrivileges()&(byte)0x80)==(byte)0x80) sb.append("Application is a Security Domain\n");
        if ((byte)(ca[index].getPrivileges()&(byte)0x40)==(byte)0x40) sb.append("Security Domain has DAP Verification capability\n");
        if ((byte)(ca[index].getPrivileges()&(byte)0x20)==(byte)0x20) sb.append("Security Domain has Delegated Management privileges\n");        
        if ((byte)(ca[index].getPrivileges()&(byte)0x10)==(byte)0x10) sb.append("Application has the privilege to lock the card\n");
        if ((byte)(ca[index].getPrivileges()&(byte)0x08)==(byte)0x08) sb.append("Application has the privilege to terminate the card\n");
        if ((byte)(ca[index].getPrivileges()&(byte)0x04)==(byte)0x04) sb.append("Application has the Default Selected privilege\n");
        if ((byte)(ca[index].getPrivileges()&(byte)0x02)==(byte)0x02) sb.append("Application has CVM management privileges\n");
        if ((byte)(ca[index].getPrivileges()&(byte)0x01)==(byte)0x01) sb.append("Security Domain has mandated DAP Verification capability\n");
       
        return sb.toString();
    }
    
    public String getSubset(int index) {
        switch (ca[index].getSubset()) {
            case (byte)0x80 : return new String("Issuer Domain");
            case (byte)0x40 : return new String("Applications and Security Domains");
            case (byte)0x20 : return new String("Executable Load Files");
        }
        return new String("undef");
    }
        
}
